package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.HashMap;
import java.util.Map;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.cucc.service.cache.CuccContextCustCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
 * <Description> 业务号码 <br>
 *
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.attr.handler <br>
 * @since R9.0<br>
 */
@Service
public class LocalGenNumberAttrHandler extends AbstractAttrHandler<KiteProdInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteProdInst inst, AttrData data) {

//            if (!CuccServiceContext.getIsCallInfForInitAttr()) {
//                return;
//            }
        // 重提初始化场景初始化过程不处理
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode")) {
            return;
        }
        // 二次业务的场景初始化过程不处理
        if (!KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK, ServiceOfferConsts.SERVICE_OFFER_ORDER)
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return;
        }
        // 数据来源是订单中心，不需要调接口生成号码
        String sceneInstId = data.getSceneInstId();
        String askSource = querySceneInstService.getSceneInst(sceneInstId).getAskSource();
        if (KiteStringUtils.equals(LocalKeyConsts.ASK_SOURCE_ORDER, askSource)) {
            return;
        }
        if (ActionType.A.equals(inst.getActionType()) && KiteStringUtils.isEmpty(attr.getValue())) {
            this.createSerialNumber(attr, inst, data);
        }
    }

    @Override
    public void set(KiteAttr attr, KiteProdInst inst, String value, AttrData data) {
        super.set(attr,inst,value,data);
        //模版还原时，需要生成新的标识
        if ("sceneTempl".equals(data.getExtAttributes().get("entry"))) {
            this.createSerialNumber(attr, inst, data);
        }
    }

    public void createSerialNumber(KiteAttr attr, KiteProdInst inst, AttrData data) {
        String sceneInstId = data.getSceneInstId();
        // 序列
        Map<String, String> inParam = new HashMap<>();
        CuccContextCustDTO custDTO = CuccContextCustCache.get(data.getStaffCode(), data.getCustId());
        inParam.put("SEQ_TYPE", "SERIAL_NUMBER");
        inParam.put("SYS_TYPE", "1");
        inParam.put("SYS_SOURCE", "");
        String seq = LocalQuerySeqHelper.getSeqBySeqType(inParam, sceneInstId, inst.getProdInstId(), custDTO);
        if (KiteStringUtils.isEmpty(seq)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("调用户中心接口获取业务号码失败");
        }
        attr.setValue(seq);
        attr.setValueDesc(seq);
        attr.setDefaultValue(seq);
        super.set(attr, inst, seq, data);
    }
}
